/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "DebugRender.h"
#include "../Content/VertexShader.h"
#include "../Content/PixelShader.h"

namespace Hogshead
{
	namespace DX11Rendering
	{
//		ProcessCPP(Hogshead::DX11Rendering::DebugRender)
//
//		DebugRender::DebugRender(void)
//		{
//			_enabled = true;
//			_wireframe_rasterizer = NULL;
//			_debug_cylinder_vertex_buffer = NULL;
//			_debug_sphere_vertex_buffer = NULL;
//			_debug_vs = NULL;
//			_debug_ps = NULL;
//		}
//
//
//		DebugRender::~DebugRender(void)
//		{
//		}
//
//		void DebugRender::finalize()
//		{
//			if(_wireframe_rasterizer) _wireframe_rasterizer->Release();
//			if(_debug_cylinder_vertex_buffer) _debug_cylinder_vertex_buffer->Release();
//			if(_debug_sphere_vertex_buffer) _debug_sphere_vertex_buffer->Release();
//				
//			IEnumerator<Pair<int, ID3D11Buffer*>>& hash_enum = _tri_vertices.getEnumerator();
//			while(hash_enum.moveNext())
//				hash_enum.current().second->Release();
//
//			if(_debug_vs) ContentLibrary::release( _debug_vs );
//			if(_debug_ps) ContentLibrary::release( _debug_ps );
//		}
//
//		void DebugRender::initialize()
//		{
//			createSphere();
//			createCylinder();
//			
//			D3D11_RASTERIZER_DESC rd;
//			rd.AntialiasedLineEnable = true;
//			rd.CullMode = D3D11_CULL_NONE;
//			rd.DepthBias = 0;
//			rd.DepthBiasClamp = 0.0f;
//			rd.DepthClipEnable = true;
//			rd.FillMode = D3D11_FILL_WIREFRAME;
//			rd.ScissorEnable = false;
//			rd.FrontCounterClockwise = true;
//			rd.MultisampleEnable = false;
//			rd.SlopeScaledDepthBias = 0.0f;
//
//			DX11Manager::createRasterizerState(&rd, &_wireframe_rasterizer);
//#ifdef _DEBUG
//			const char debug_string[] = "debug sphere vertex buffer";
//			HRESULT hr = _wireframe_rasterizer->SetPrivateData( WKPDID_D3DDebugObjectName, sizeof(debug_string) - 1, debug_string );
//			if(FAILED(hr))
//			{
//				StringBuilder sb;
//				sb << "ERROR ON CONTENT LOAD: debug sphere vertex buffer could not be named";
//				DX11Manager::_logger->write(sb);
//				throw new Exception(sb);
//			}
//#endif
//		}
//
//		void DebugRender::createSphere()
//		{
//			//create the sphere vertex buffer
//			Vector<PointVertex> _sphere_vertices;
//			//x-y
//			for( int i = 0; i < 100; i++ )
//			{
//				_sphere_vertices.add( PointVertex(Math::cos(2.0f * Math::Pi * i/100.0f), Math::sin(2.0f * Math::Pi * i/100.0f), 0.0f) );
//			}
//			//y-z
//			for( int j = 0; j < 100; j++ )
//			{
//				_sphere_vertices.add( PointVertex(0.0f, Math::cos(2.0f * Math::Pi * j/100.0f), Math::sin(2.0f * Math::Pi * j/100.0f)) );
//			}
//			//z-x
//			for (int k = 0; k < 100; k++ )
//			{
//				_sphere_vertices.add( PointVertex(Math::sin(2.0f * Math::Pi * k/100.0f), 0.0f, Math::cos(2.0f * Math::Pi * k/100.0f) ) );
//			}
//
//			//now the actual buffer
//			//_debug_sphere_vertex_buffer
//			D3D11_BUFFER_DESC bd;
//			ZeroMemory(&bd, sizeof(bd));
//			bd.Usage = D3D11_USAGE_DEFAULT;
//			bd.ByteWidth = sizeof(PointVertex) * (_sphere_vertices.size());
//			bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
//			bd.CPUAccessFlags = 0;
//			D3D11_SUBRESOURCE_DATA InitData;
//			ZeroMemory(&InitData, sizeof(InitData));
//			InitData.pSysMem = (const PointVertex*)(_sphere_vertices);
//			HRESULT hr;
//
//			hr = DX11Manager::createBuffer(&bd, &InitData, &_debug_sphere_vertex_buffer);
//			if( FAILED( hr ) ) return;
//#ifdef _DEBUG
//			const char debug_string[] = "debug sphere vertex buffer";
//			hr = _debug_sphere_vertex_buffer->SetPrivateData( WKPDID_D3DDebugObjectName, sizeof(debug_string) - 1, debug_string );
//			if(FAILED(hr))
//			{
//				StringBuilder sb;
//				sb << "ERROR ON CONTENT LOAD: debug sphere vertex buffer could not be named";
//				DX11Manager::_logger->write(sb);
//				throw new Exception(sb);
//			}
//#endif
//		}
//
//		void DebugRender::createCylinder()
//		{
//			//create the sphere vertex buffer
//			Vector<PointVertex> _cylinder_vertices;
//			//x-y circle top
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(Math::cos(2.0f * Math::Pi * i/100.0f), Math::sin(2.0f * Math::Pi * i/100.0f), 0.5f) );
//			}
//			//x-y circle center
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(Math::cos(2.0f * Math::Pi * i/100.0f), Math::sin(2.0f * Math::Pi * i/100.0f), 0.0f) );
//			}
//			//x-y circle bottom
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(Math::cos(2.0f * Math::Pi * i/100.0f), Math::sin(2.0f * Math::Pi * i/100.0f), -0.5f) );
//			}
//			
//			//center
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(0.0f, 0.0f,  i/100.0f - 0.5f) );
//			}
//			//side
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(1.0f, 0.0f,  i/100.0f - 0.5f) );
//			}
//			//side
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(0.0f, 1.0f,  i/100.0f - 0.5f) );
//			}
//			//side
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(-1.0f, 0.0f,  i/100.0f - 0.5f) );
//			}
//			//side
//			for( int i = 0; i < 100; i++ )
//			{
//				_cylinder_vertices.add( PointVertex(0.0f, -1.0f,  i/100.0f - 0.5f) );
//			}
//
//			//now the actual buffer
//			//_debug_sphere_vertex_buffer
//			D3D11_BUFFER_DESC bd;
//			ZeroMemory(&bd, sizeof(bd));
//			bd.Usage = D3D11_USAGE_DEFAULT;
//			bd.ByteWidth = sizeof(PointVertex) * (_cylinder_vertices.size());
//			bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
//			bd.CPUAccessFlags = 0;
//			D3D11_SUBRESOURCE_DATA InitData;
//			ZeroMemory(&InitData, sizeof(InitData));
//			InitData.pSysMem = (const PointVertex*)(_cylinder_vertices);
//			HRESULT hr;
//
//			hr = DX11Manager::createBuffer(&bd, &InitData, &_debug_cylinder_vertex_buffer);
//			if( FAILED( hr ) ) return;
//#ifdef _DEBUG
//			const char debug_string[] = "debug cylinder vertex buffer";
//			hr = _debug_cylinder_vertex_buffer->SetPrivateData( WKPDID_D3DDebugObjectName, sizeof(debug_string) - 1, debug_string );
//			if(FAILED(hr))
//			{
//				StringBuilder sb;
//				sb << "ERROR ON CONTENT LOAD: debug sphere vertex buffer could not be named";
//				DX11Manager::_logger->write(sb);
//				throw new Exception(sb);
//			}
//#endif
//		}
//
//		void DebugRender::createLineBuffers(Vector<Vector<Vector3>*>* in_lines, LineList* in_render_info)
//		{
//			Vector<ID3D11Buffer*> _all_buffers;
//			for(int i = 0; i < in_lines->size(); i++)
//			{
//				Vector<PointVertex> vertices;
//				for(int j = 0; j < in_lines->get(i)->size(); j++)
//				{
//					vertices.add(PointVertex(in_lines->get(i)->get(j)));
//				}
//
//				ID3D11Buffer* _line_list_buffer;
//				D3D11_BUFFER_DESC bd;
//				ZeroMemory(&bd, sizeof(bd));
//				bd.Usage = D3D11_USAGE_DEFAULT;
//				bd.ByteWidth = sizeof(PointVertex) * (vertices.size());
//				bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
//				bd.CPUAccessFlags = 0;
//				D3D11_SUBRESOURCE_DATA InitData;
//				ZeroMemory(&InitData, sizeof(InitData));
//				InitData.pSysMem = (const PointVertex*)(vertices);
//				HRESULT hr;
//
//				hr = DX11Manager::createBuffer(&bd, &InitData, &_line_list_buffer);
//				if( FAILED( hr ) ) return;
//
//				_all_buffers.add(_line_list_buffer);
//				_line_sizes.add(vertices.size());
//			}
//
//			_line_buffers[(int)in_render_info] = _all_buffers;
//		}
//
//		void DebugRender::registerComponent(IComponent* in_comp)
//		{
//#ifdef _DEBUG
//			if(in_comp->getDebugRenderInfo())
//			{
//				IRenderInfo* ri = in_comp->getDebugRenderInfo();
//				if(ri->is<TriangleWireframe>() ||
//					ri->is<SphereWireframe>() ||
//					ri->is<CylinderWireframe>() ||
//					ri->is<CapsuleWireframe>())
//				{
//					_registered_components.add(in_comp);
//					if(ri->is<TriangleWireframe>())
//					{
//						this->setTriangle(in_comp->getDebugRenderInfo()->as<TriangleWireframe>());
//					}
//				}
//			}
//#endif
//		}
//
//
//		void DebugRender::setTriangle(TriangleWireframe* in_triangle)
//		{
//			PointVertex data[3];
//			for(int i = 0; i < 3; i++)
//			{
//				data[i] = in_triangle->vertex_list[i];
//			}
//
//			ID3D11Buffer* vertex_buffer;
//			D3D11_BUFFER_DESC bd;
//			ZeroMemory(&bd, sizeof(bd));
//			bd.Usage = D3D11_USAGE_DEFAULT;
//			bd.ByteWidth = sizeof(PointVertex) * 3;
//			bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
//			bd.CPUAccessFlags = 0;
//			D3D11_SUBRESOURCE_DATA InitData;
//			ZeroMemory(&InitData, sizeof(InitData));
//			InitData.pSysMem = data;
//			HRESULT hr;
//
//			hr = DX11Manager::createBuffer(&bd, &InitData, &vertex_buffer);
//
//			_tri_vertices[(int)in_triangle] = vertex_buffer;
//
//			
//		}
//
//		void DebugRender::render(HashTable<int, Pair<ID3D11RenderTargetView*, ID3D11ShaderResourceView*>*>* in_render_targets, Camera* in_active_camera)	
//		{
//#ifdef _DEBUG
//			DX11Manager::setRenderTargets(1, &(*in_render_targets)[DX11Director::BACK_BUFFER]->first, DX11Manager::_depth_stencil_view);
//			//clear the depthstencil buffer
//			DX11Manager::clearDepthStencil(DX11Manager::_depth_stencil_view, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
//			
//			//DX11Manager::setTopology( D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP );
//			//set vertex input
//			DX11Manager::setInputLayout( _debug_vs->_input_layout );
//			//set shaders
//			DX11Manager::setShaders( _debug_vs->_vs, NULL, _debug_ps->_ps );
//
//			if( in_active_camera )
//			{
//				DX11Manager::_basic_dynamic_struct._view = in_active_camera->getView();
//			}
//			else
//			{
//				DX11Manager::_basic_dynamic_struct._view = Matrix4();
//				throw Exception("You need an active camera to render");
//			}
//
//			//DX11Manager::setRasterizerState(_wireframe_rasterizer);
//
//			IRenderInfo* render_info;
//
//			for( int i = 0; i < _registered_components.size(); i++)
//			{
//				Vector3 comp_pos = _registered_components[i]->getParentGameObject()->getTransform().getWorldPosition();
//				if(Vector3::distanceSquared(in_active_camera->getParentGameObject()->getTransform().getWorldPosition(), comp_pos) < 900000000)
//				{
//					DX11Manager::setRasterizerState(_wireframe_rasterizer);
//				
//					render_info = _registered_components[i]->getDebugRenderInfo();
//					if(!render_info)
//						continue;
//
//					if(render_info->is<SphereWireframe>())
//					{
//						//set topo
//					DX11Manager::setTopology( D3D11_PRIMITIVE_TOPOLOGY_POINTLIST );
//						//render the debug sphere
//						//get the constant buffer
//
//						SphereWireframe* sphere = render_info->as<SphereWireframe>();
//						DX11Manager::_basic_dynamic_struct._world = sphere->world;
//						memcpy( DX11Manager::_basic_dynamic_struct._ambient_color, (float*)sphere->color, 4 * sizeof(float));
//
//						//update constant buffer
//						DX11Manager::updateConstantBuffer(BASIC_DYNAMIC, &DX11Manager::_basic_dynamic_struct);
//						//update vertex buffer
//						DX11Manager::setVertexBuffer(_debug_sphere_vertex_buffer, sizeof(PointVertex) );
//						//draw
//						DX11Manager::draw(300, 0);
//					}
//
//					if(render_info->is<CylinderWireframe>())
//					{
//						//set topo
//						DX11Manager::setTopology( D3D11_PRIMITIVE_TOPOLOGY_POINTLIST );
//						DX11Manager::setRasterizerState(_wireframe_rasterizer);
//
//						//render the debug cylinder
//						//get the constant buffer
//						CylinderWireframe* cylinder = render_info->as<CylinderWireframe>();
//						DX11Manager::_basic_dynamic_struct._world = cylinder->world;
//						memcpy( DX11Manager::_basic_dynamic_struct._ambient_color, (float*)cylinder->color, 4 * sizeof(float));
//
//						//update constant buffer
//						DX11Manager::updateConstantBuffer(BASIC_DYNAMIC, &DX11Manager::_basic_dynamic_struct);
//						//update vertex buffer
//						DX11Manager::setVertexBuffer(_debug_cylinder_vertex_buffer, sizeof(PointVertex) );
//						//draw
//						DX11Manager::draw(800, 0);
//					}
//				
//					if(render_info->is<CapsuleWireframe>())
//					{
//						//render the debug capsule, in 3 parts
//						//get the constant buffer
//						CapsuleWireframe* capsule = render_info->as<CapsuleWireframe>();
//						DX11Manager::setRasterizerState(_wireframe_rasterizer);
//
//						//cylinder
//						DX11Manager::_basic_dynamic_struct._world = capsule->cylinder_world;
//						memcpy( DX11Manager::_basic_dynamic_struct._ambient_color, (float*)capsule->color, 4 * sizeof(float));
//
//						//update the constant buffer
//						DX11Manager::updateConstantBuffer(BASIC_DYNAMIC, &DX11Manager::_basic_dynamic_struct);
//						//update the vertex buffer
//						DX11Manager::setVertexBuffer(_debug_cylinder_vertex_buffer, sizeof(PointVertex) );
//						//draw
//						DX11Manager::draw( 800, 0);
//
//
//						//front
//						DX11Manager::_basic_dynamic_struct._world = capsule->front_sphere_world;
//					
//						//update the constant buffer
//						DX11Manager::updateConstantBuffer(BASIC_DYNAMIC, &DX11Manager::_basic_dynamic_struct);
//						//update the vertex buffer
//						DX11Manager::setVertexBuffer(_debug_sphere_vertex_buffer, sizeof(PointVertex) );
//						//draw
//						DX11Manager::draw( 300, 0);
//
//
//						//bottom
//						DX11Manager::_basic_dynamic_struct._world = capsule->back_sphere_world;
//					
//						//update the constant buffer
//						DX11Manager::updateConstantBuffer(BASIC_DYNAMIC, &DX11Manager::_basic_dynamic_struct);
//						//update the vertex buffer
//						DX11Manager::setVertexBuffer(_debug_sphere_vertex_buffer, sizeof(PointVertex) );
//						//draw
//						DX11Manager::draw( 300, 0);
//
//
//					}
//
//					if(render_info->is<TriangleWireframe>())
//					{
//						DX11Manager::setTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//						TriangleWireframe* triangle = (TriangleWireframe*)render_info;
//
//						DX11Manager::setRasterizerState(this->_wireframe_rasterizer);
//						ID3D11Buffer* cur_buffer = _tri_vertices[(int)render_info];
//						DX11Manager::setVertexBuffer(cur_buffer, sizeof(PointVertex));
//						////render the debug cylinder
//						////get the constant buffer
//						DX11Manager::_basic_dynamic_struct._world = triangle->world;
//						memcpy( DX11Manager::_basic_dynamic_struct._ambient_color, (float*)triangle->color, 4 * sizeof(float));
//
//						////update constant buffer
//						DX11Manager::updateConstantBuffer(BASIC_DYNAMIC, &DX11Manager::_basic_dynamic_struct);
//
//					
//
//						DX11Manager::draw(3,0);
//					}
//					if(render_info->is<LineList>())
//					{
//						DX11Manager::setTopology(D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);
//						LineList* line_list = (LineList*)render_info;
//						DX11Manager::setRasterizerState(_wireframe_rasterizer);;
//
//						Vector<ID3D11Buffer*> _buffers = _line_buffers[(int)render_info];
//
//						DX11Manager::_basic_dynamic_struct._world = line_list->world;
//						memcpy( DX11Manager::_basic_dynamic_struct._ambient_color, (float*)line_list->color, 4 * sizeof(float));
//
//						////update constant buffer
//						DX11Manager::updateConstantBuffer(BASIC_DYNAMIC, &DX11Manager::_basic_dynamic_struct);
//
//						for(int j = 0; j < _buffers.size(); j++)
//						{
//							DX11Manager::setVertexBuffer(_buffers[j], sizeof(PointVertex));
//							//DX11Manager
//						}
//
//					}
//				}
//			}
//			return;
//#endif
//		}
//
//		IProcess* DebugRender::readXML( const String& in_xml )
//		{
//			//get the two we know we want always
//			_debug_vs = (VertexShader*) ContentLibrary::loadAndParseContentXML("Content/Shaders/BasicShader.fx.VS.xml");
//			_debug_ps = (PixelShader*) ContentLibrary::loadAndParseContentXML("Content/Shaders/BasicShader.fx.PS.xml");
//
//			return this;
//		}

	}
}